package com.ruoyi.web.controller.cashier;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.MemberLevel;
import com.ruoyi.system.service.IMemberLevelService;
import com.ruoyi.system.util.StoreIdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("member-level")
@Api(tags = "会员等级权益管理")
public class MemberLevelController {

    @Autowired
    private IMemberLevelService memberLevelService;

    @Autowired
    private StoreIdUtils storeIdUtils;

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询会员等级")
    public AjaxResult getMemberLevelById(@PathVariable Integer id) {
        return AjaxResult.success(memberLevelService.getById(id));
    }

    @GetMapping
    @ApiOperation("分页查询会员等级列表")
    public AjaxResult list(MemberLevel params, Page<MemberLevel> page) {
        LambdaQueryWrapper<MemberLevel> wrapper = new LambdaQueryWrapper<>();
        if (params.getLevelName() != null) wrapper.like(MemberLevel::getLevelName, params.getLevelName());
        if (params.getStatus() != null) wrapper.eq(MemberLevel::getStatus, params.getStatus());
        return AjaxResult.success(memberLevelService.page(page, wrapper));
    }

    @PostMapping
    @ApiOperation("新增会员等级")
    public AjaxResult create(@RequestBody MemberLevel memberLevel) {
        memberLevelService.save(memberLevel);
        return AjaxResult.success();
    }

    @PutMapping
    @ApiOperation("更新会员等级")
    public AjaxResult update(@RequestBody MemberLevel memberLevel) {
        memberLevelService.updateById(memberLevel);
        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除会员等级")
    public AjaxResult delete(@PathVariable Integer id) {
        memberLevelService.removeById(id);
        return AjaxResult.success();
    }

    /**
     * 会员等级列表
     */
    @GetMapping("/list")
    @ApiOperation("会员等级列表")
    public AjaxResult list() {
        LambdaQueryWrapper<MemberLevel> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MemberLevel::getStoreId, storeIdUtils.getStoreId());
        wrapper.eq(MemberLevel::getStatus,"ACTIVE");
        List<MemberLevel> list = memberLevelService.list(wrapper);
        return AjaxResult.success(list);
    }
}
